package com.song.myrule;


/*
 *
 * Copyright 2013 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
//package com.netflix.loadbalancer;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 自定义随机规则：
 *      每个服务器可访问5次,访问超过5次时，换下一个服务
 *      现在有2个 provicder 服务器
 *
 *
 */
public class SongRandomRule extends AbstractLoadBalancerRule {

    private int visitCount = 0;

    //通过自定义获取规则，从所有服务器中选择一个可访问的
    public Server choose(ILoadBalancer lb, Object key) {
        System.out.println("进入了宋大米定制的负载均衡规则！！！");
        if (lb == null) {
            return null;
        }
        Server server = null;

        while (server == null) {
            if (Thread.interrupted()) {
                return null;
            }
            // 获取可访问服务器、所有服务
            List<Server> upList = lb.getReachableServers();
            List<Server> allList = lb.getAllServers();

            int serverCount = allList.size();
            if (serverCount == 0) {
                return null;
            }
            // 随机选择一个可访问的服务
            //int index = chooseRandomInt(serverCount);
            int index = 0;
            server = upList.get(index);

            if (server == null) {
                Thread.yield();
                continue;
            }
            // 如果服务存活，判断访问次数
            if (server.isAlive()) {
                // 小于5次，增加访问次数，并返回服务；
                if (visitCount < 5){
                    visitCount ++;
                    System.out.println("get server=>"+server+",index=>"+index+",visitCount=>"+visitCount);
                    return server;
                }else {
                    //大于5次，将访问次数重置为0，切换到下一个可访问的服务器
                    index = (index + 1) % serverCount;
                    visitCount = 0;
                    server = upList.get(index);
                }
                System.out.println("get server=>"+server+",index=>"+index+",visitCount=>"+visitCount);
                return (server);
            }
            server = null;
            Thread.yield();
        }
        return server;
    }

    protected int chooseRandomInt(int serverCount) {
        return ThreadLocalRandom.current().nextInt(serverCount);
    }

    @Override
    public Server choose(Object key) {
        return choose(getLoadBalancer(), key);
    }

    @Override
    public void initWithNiwsConfig(IClientConfig clientConfig) {
        // TODO Auto-generated method stub

    }

}
